package com.ruoyi.web.controller.veterinary;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.veterinary.domain.VetInventory;
import com.ruoyi.veterinary.service.IVetInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/veterinary/inventory")
public class VetInventoryController extends BaseController
{
    @Autowired
    private IVetInventoryService vetInventoryService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(VetInventory vetInventory)
    {
        startPage();
        List<VetInventory> list = vetInventoryService.selectVetInventoryList(vetInventory);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VetInventory vetInventory)
    {
        List<VetInventory> list = vetInventoryService.selectVetInventoryList(vetInventory);
        ExcelUtil<VetInventory> util = new ExcelUtil<VetInventory>(VetInventory.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:query')")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable("inventoryId") Long inventoryId)
    {
        return success(vetInventoryService.selectVetInventoryByInventoryId(inventoryId));
    }

    /**
     * 查询即将过期药品列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:list')")
    @GetMapping("/expiring/{days}")
    public AjaxResult getExpiringList(@PathVariable Integer days)
    {
        List<VetInventory> list = vetInventoryService.selectExpiringDrugList(days);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有库存商品（用于快速选择）
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:list')")
    @GetMapping("/all")
    public AjaxResult getAllInventoryItems()
    {
        VetInventory vetInventory = new VetInventory();
        List<VetInventory> list = vetInventoryService.selectVetInventoryList(vetInventory);
        return AjaxResult.success(list);
    }

    /**
     * 根据关键词搜索库存商品
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:list')")
    @GetMapping("/search")
    public AjaxResult searchInventoryItems(@RequestParam String keyword)
    {
        List<VetInventory> list = vetInventoryService.searchInventoryByKeyword(keyword);
        return AjaxResult.success(list);
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VetInventory vetInventory)
    {
        return toAjax(vetInventoryService.updateVetInventory(vetInventory));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:inventory:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds)
    {
        return toAjax(vetInventoryService.deleteVetInventoryByInventoryIds(inventoryIds));
    }

    /**
     * 查询库存预警列表
     */
    @GetMapping("/warning")
    public AjaxResult getWarningList()
    {
        List<VetInventory> list = vetInventoryService.selectInventoryWarningList();
        return success(list);
    }



    /**
     * 根据药品名称和批号查询库存
     */
    @GetMapping("/search/{drugName}/{batchNumber}")
    public AjaxResult searchByDrugAndBatch(@PathVariable("drugName") String drugName, 
                                          @PathVariable("batchNumber") String batchNumber)
    {
        VetInventory inventory = vetInventoryService.selectInventoryByDrugAndBatch(drugName, batchNumber);
        return success(inventory);
    }
}